package com.qxzn.app.medicalhealth.util;
 
import android.content.Context;
import android.support.v7.widget.LinearSmoothScroller;
import android.view.View;
 
/**
 * @description recyclerView滚动器
 */
public class DetailRvSmoothScroller extends LinearSmoothScroller {
 
    private View mTitleBar;
 
    public DetailRvSmoothScroller(Context context, View titleBar) {
        super(context);
        this.mTitleBar = titleBar;
    }
 
    /**
     * 指定滚动停留位置
     * @return {@link #LinearSmoothScroller#SNAP_TO_START}，{@link #LinearSmoothScroller#SNAP_TO_END},{@link #LinearSmoothScroller#SNAP_TO_ANY}
     * 1.将子视图的左侧或顶部与父视图的左侧或顶部对齐；
     * 2.将子视图的右侧或底部与父视图的右侧或底部对齐；
     * 3.具体取决于其当前与其父代相关的位置,也是默认设置。
     */
    @Override
    protected int getVerticalSnapPreference() {
        return LinearSmoothScroller.SNAP_TO_START;
    }
 
    /**
     * 计算最终需要滚动的距离
     * @param viewStart
     * @param viewEnd
     * @param boxStart
     * @param boxEnd
     * @param snapPreference
     * @return
     */
    @Override
    public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
        switch (snapPreference) {
            case SNAP_TO_START:
                return boxStart - viewStart + mTitleBar.getMeasuredHeight();
            case SNAP_TO_END:
                return boxEnd - viewEnd;
            case SNAP_TO_ANY:
                final int dtStart = boxStart - viewStart;
                if (dtStart > 0) {
                    return dtStart;
                }
                final int dtEnd = boxEnd - viewEnd;
                if (dtEnd < 0) {
                    return dtEnd;
                }
                break;
            default:
                throw new IllegalArgumentException("snap preference should be one of the"
                        + " constants defined in SmoothScroller, starting with SNAP_");
        }
        return 0;
    }
}
